Basic Git commands
December 09, 2019
Basic git commands
git config
Your Identity
git config --global user.name "John Doe"
git config --global user.email johndoe@example.comConfigure Proxy
git config --global --add http.proxy http://webproxy.dnb.no:80/
git config --global --add https.proxy http://webproxy.dnb.no:80/Unset Git config
git config --global --unset https.proxy
git config --global --unset http.proxyChecking your settings
git config --list
git config global --list
git config system --list
git config local --listChecking specific key’s value
in your config settings
git config <key>
git config user.name1. git help
Getting Help
git help <verb>
git <verb> --help
man git-<verb>
git help config2. git clone
clone an existing git repository.
git clone <remote-repository-url>
git clone <remote-repository-url> <custom-folder-name>
git clone https://github.com/libgit2/libgit2clone a specific branch. Using the -b option, you are fetching all the branches but you are checking out the branch you chose.
git clone -b <branch> <remote_repo>
git clone -b dev https://github.com/username/project.git3. git init
initialize a git repository
git init4. git status
Checking the Status of Your Files
git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean5. git add
Tracking New Files or Staging Modified files
git add <file-name>To stage all modified files / track all new files
git add .6. git commit
commit your changes to git repository
git commit -m "Story 182: Fix benchmarks for speed"Skip staging area by using -a option to the git commit command, this will make Git automatically stage every file that is already tracked before doing the commit, letting you skip the git add part
git commit -a -m 'added new benchmarks'7. git rm
Removing Files from Git. git rm removes file from working directory
git rm <file-name>
git rm PROJECTS.md8. git mv
Moving Files. To rename a file in Git, you can run
git mv <file-from> <file-to>
git mv README.md READMEIgnoring files
in git repository .gitignore file
The rules for the patterns you can put in the .gitignore file are as follows:
- Blank lines or lines starting with # are ignored.
- Standard glob patterns work.
- You can end patterns with a forward slash (/) to specify a directory.
- You can negate a pattern by starting it with an exclamation point (!). Glob patterns are like simplified regular expressions that shells use.
- An asterisk (*) matches zero or more characters;
- [abc] matches any character inside the brackets (in this case a, b, or c);
- a question mark (?) matches a single character; and
- brackets enclosing characters separated by a hyphen([0-9]) matches any character in the range (in this case 0 through 9) .
Here is an example .gitignore file:
# a comment - this is ignored
# no .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the root TODO file, not subdir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .txt files in the doc/ directory
doc/**/*.txt
A **/ pattern is available in Git since version 1.8.2.9. git diff
To see what you have changed but not yet staged
git diff To see what you have staged that will go into your next commit
git diff --cached (or git diff --staged)diff between the tips of the two branches
git diff branch_1..branch_2to find the diff from their common ancestor to test, you can use three dots instead of two Git now compares the tip of our feature branch with the common ancestor commit of both branches
git diff branch_1...branch_2And if you just want to check which files differ, not how the content differs. Use this:
git diff --name-only branch_1...branch_210. git log
Viewing the Commit History.
git log
git log -p -2
git log --stat
git log --pretty=oneline
git log --pretty=format:"%h - %an, %ar : %s"
git log --pretty=format:"%h %s" --graph
git log --since=2.weeks
git log --oneline --decorate --graph --all11. git remote
Working with Remotes. Manage remote repositories.
Showing Your Remotes.
git remote
origin
git remote -v
origin https://github.com/schacon/ticgit (fetch)
origin https://github.com/schacon/ticgit (push)Adding Remote Repositories.
Note:
git clonecommand implicitly adds the origin remote for you. To add a new remote Git repository as a shortname you can reference easily
git remote add <shortname> <url>
git remote add pb https://github.com/paulboone/ticgitInspecting a Remote. use the following command to see more information about a particular remote
git remote show <remote>
git remote show originRenaming Removing Remotes Following command will rename the pb remote to paul. this changes all your remote-tracking branch names, too. pb/master is now paul/master
git remote rename pb paulTo remove a remote use following command
git remote remove paul
git remote rm paul12. git fetch
Fetching and Pulling from Your Remotes. To get data from your remote project you can run
git fetch <remote>
git fetch originNote:
git fetchcommand only downloads the data to your local repository - it doesn’t automatically merge it with any of your work or modify what you’re currently working on. You have to merge it manually into your work when you’re ready.
13. git pull
git pull command is used to automatically fetch and then merge the remote branch into your current branch. Running git pull generally fetches data from server you originally cloned from and automatically tries to merge it into the code you’re currently working on.
git pull14. git push
Pushing to Your Remotes. push committed changes in local repository up to remote repository.
git push
git push <remote> <branch>
git push origin masterShare changes (this will create branchname on remote repo if it doesn’t exist)
git push originPush a new local branch to a remote git repository and track it too (-u option tells git to start tracking local branch to branchname from origin) After this one time setup of tracking information git push can be directly used to share updates quickly and easily
git push -u origin feature_branch_name15. git tag
Tagging.
Listing Your Tags.
Listing tag wildcards requires -l or --list option
git tag
git tag -l "v1.8.5*"Annotated Tags
git tag -a v1.4 -m "my version 1.4"Lightweight Tags
git tag v1.4-lwTo see tag information
git show v1.4
git show 1.4-lwTagging Later
git tag -a <tag-name> <commit-hash>
git tag -a v1.2 9fceb02Sharing Tags
git push origin v1.5
git push origin --tagsDeleting Tags
git tag -d v1.4-lwTo delete a remote tag
git push origin --delete <tagname>Checkout Tags
git checkout v2.0.0
git checkout -b version2 v2.0.016. git branch
Branch Management.
List your current branches
git branchTo see last commit on each branch
git branch -vThe useful --merged and --no-merged options can filter the list of branches that you have not yet merged into the branch you’re currently on.
git branch --merged
git branch --no-mergedCreate a new branch
git branch testingIf a branch exists on the remote repository, but not on your local repo, you can simply use git switch. Note that if remote branch doesn’t exist locally you’ll need to git fetch first before using switch.
git switch <remote-repo-branch>Delete a branch
git branch -d hotfixDelete a branch from remote repo
git branch -a
git push origin --delete hotfixDelete an un-merged branch or delete a branch and lose the work
git branch -D testing17. git checkout
Switching Branches
git checkout testingCreating a new branch and switching to it at the same time
git checkout -b <newbranchname>18. git merge
merge branch to checked out branch
git checkout master
git merge hotfix19. git rebase
to maintain topic branches
A---B---C topic
/
D---E---F---G mastergit rebase master
git rebase master topic A'--B'--C' topic
/
D---E---F---G masterNOTE: The latter form is just a short-hand of git checkout topic followed by git rebase master. When rebase exits topic will remain the checked-out branch.
After resolving the conflict manually and updating the index with the desired resolution, you can continue the rebasing process with
git rebase --continueYou can undo git rebase with
git rebase --abort