-
Notifications
You must be signed in to change notification settings - Fork 288
git Commands
Below is a list of common git commands organized by function. For a good tutorial, check out this resource by AtLassian.
You can clone either using an ssh or https protocol. SSH is probably more convenient overall because you do not need to log your credentials if you are a contributor to the repository, but you neet to set up ssh-keys, which requires following a few steps initially. HTTPS is easier initially because you do not need any user credentials, but to commit to the main repo, you'll need to log on.
The two approaches are given below
git clone [email protected]:MPC-Berkeley/barc.git # ssh method
git clone https://github.com/MPC-Berkeley/barc.git # https method
git add . (this adds all the file)
git commit -m "Commit message"
git fetch (Attach all the on-line branches, now you can checkout)
git push origin BRANCH-NAME (It is always better to push a single branch)
git reset *.mp4 (This removes from the staging are all the file *.mp4 so that they do not get pushed to GitHub)
git branch NewBranch
git branch -a (Shows the branches online, very useful)
git checkout NewBranch
git push --set-upstram origin NewBranch
git checkout master
git pull
git merge NewBranch
git branch -d NewBranch
git push origin --delete NewBranch
git log --oneline (This shows the commit_ID)
git diff
git checkout commit_ID (This brings you back to that particular commit)
Need to get ID of desired ommit
git reset --hard commit_ID
Go on the BARC github page and fork to your repository
git clone "the forked url in your repo"
git add upstream "originl url"
git push (Need to push to your repository what you just pulled from the repository you forked)
git fetch origin "BRANCH-NAME"
git add <file> # adds file to the staging area
git add * # add all files starting from this directory
git add . # add all files in your working directory to the staging area (except for files in .gitignore)
git add <directory> # add directory
git reset <file> # removes file from staging area
git reset # removes all files from staging area -> resets staging area to most recent commit
git reset - -hard # undo changes and go back to last commit in this branch, also deletes files
git reset - -hard <commit> # move branch tip back to <commit>, everything ahead of this commit is deleted!
git revert <commit> # revert to <commit> and use this one as the next commit
git commit -m <message> # commits current state with message
git remote add <branch> # adds a branch to remote
git push -u <branch> # push upstream, add upstream tracking reference
git checkout <commit> # view branch -> make working directory match the commit
git checkout master # return to master branch
git checkout -b <new-branch> <from-this-branch> # creates a new branch from an existing branch
git branch -r # view remote branches
git branch <branch> # creates a new branch named <branch>
git fetch <remote> <branch> # imports commits from remote repository into local repo, stored as remote branches
git fetch origin # download all remote branches from the main repo
git pull <remote> # fetch remote’s copy of current branch and merge it into local copy (adds all commits)
# equivalent to: git fetch <remote> and git merge origin/<current-branch>
git pull --rebase
git push <remote> <branch>
git diff # view differences between commits. can also use difftool more more advanced stuff
git merge <branch> # merges branch <branch> to current branch
# the old branch can be deleted afterwards by git branch -d <branch>
# can also use mergetool for complicated merges
git rebase <branch> # sets the base of the current branch (where it branched off from) to the end of <branch>
# similar to merge, but moves all branch-commits ahead of the end of the mother-branch
git status
git log
gitk
- Remote = remote connection to server (remote repository)
- Origin remote: remote connection to cloned repository
- SCM = Source Control Management
- downstream = information flowing “downstream” from a repository to the local machine
- upstream = send information back “upstream” and put them in the existing repository
- staging area: files are added to this area by using the command “git add”, but you have to use the command “git commit” to record changes
- HEAD usually points to the current branch you’re working on. However, if you’re checking out a certain commit, it points to that commit rather than to a branch, this situation is called “detached HEAD state"
Branches are only pointers to commits within these branches. In that sense, they are just a method to organize and move between different commits.
- There’s a .gitignore file in your root git folder and it contains information about files that should not be synchronized with your repo. Build files, binaries, executables, logfiles,... can be added here (they just take a lot of space and no one else needs them)
- If you’re working on a Mac, add the line “.DS_Store” to the file “.gitignore” in your git root folder (or create the file if nonexistent). This makes git ignore all (invisible) .DS_Store files.