Basic git commands

Basic git commands

git config

Configure your identity

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

Configure Proxy

git config --global --add http.proxy http://proxy.domain.com:80/
git config --global --add https.proxy http://proxy.domain.com:80/

Unset Git config

git config --global --unset https.proxy
git config --global --unset http.proxy

Checking your config settings

git config --list

git config global --list
git config system --list
git config local --list

Checking specific key’s value in your config settings

git config <key>
git config user.name

1. git help

Getting Help

git help <verb>
git <verb> --help
man git-<verb>

git help config

2. 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/libgit2

clone 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.git

3. git init

initialize a git repository

git init

4. 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 clean

5. git add


Tracking new files or staging modified files

git add <file-name>

To stage all modified files / track all new files

git add .

Ignoring 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.

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.md

8. git mv

Moving Files. To rename a file in Git, you can run

git mv <file-from> <file-to>
git mv README.md README

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_2

to 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_2

And if you just want to check which files differ, not how the content differs. Use this:

git diff --name-only branch_1...branch_2

10. 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 --all

11. git remote

Working with Remotes. Manage remote repositories.

Showing Your Remotes.

Note: git clone command implicitly adds the origin remote for you.

git remote
origin

git remote -v
origin	https://github.com/schacon/ticgit (fetch)
origin	https://github.com/schacon/ticgit (push)

Adding Remote Repositories.

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/ticgit

Inspecting a Remote. use the following command to see more information about a particular remote

git remote show <remote>
git remote show origin

Renaming 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 paul

To remove a remote use following command

git remote remove paul
git remote rm paul

12. git fetch

Fetching and Pulling from Your Remotes. To get data from your remote project you can run

git fetch <remote>
git fetch origin

Note: git fetch command 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 pull

14. 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 master

Share changes (this will create branchname on remote repo if it doesn’t exist)

git push origin

Push 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_name

15. 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-lw

To see tag information

git show v1.4
git show 1.4-lw

Tagging Later

git tag -a <tag-name> <commit-hash>
git tag -a v1.2 9fceb02

Sharing Tags

git push origin v1.5
git push origin --tags

Deleting Tags

git tag -d v1.4-lw

To delete a remote tag

git push origin --delete <tagname>
git push origin --delete v1.4-lw 

Checkout Tags

git checkout v2.0.0
git checkout -b version2 v2.0.0

16. git branch

Branch Management

List your current branches

git branch

To see last commit on each branch

git branch -v

The 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-merged

Create a new branch

git branch test

If 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 fetch 

git switch <remote-repo-branch>
git switch dev

Delete a branch

git branch -d hotfix

Delete a branch from remote repo

git branch -a
git push origin --delete hotfix

Delete an un-merged branch or delete a branch and lose the work

git branch -D testing

17. git checkout

Switching Branches

git checkout test

Creating a new branch and switching to it at the same time

git checkout -b <newbranchname>
git checkout -b dev 

18. git merge

merge branch to checked out branch

git checkout master
git merge hotfix

19. git rebase

Reapply commits on top of another base tip

Assume the following history exists and the current branch is "topic":

    A---B---C topic
    /
D---E---F---G master

From this point, the result of either of the following commands:

git rebase master
git rebase master topic

would be:

            A'--B'--C' topic
            /
D---E---F---G master

NOTE: 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 --continue

You can undo git rebase with

git rebase --abort