Engineering

Git Commands

Knowledge of version control systems are a mandatory skill set for developers.

Git is among the most popular.

It is a is a free and open source distributed version control system designed to handle any size software development project, however, it can also keep track of changes to any type of files.

If you want to create your own remote Git repositories you might want to check out https://github.com/ and https://bitbucket.org/. Both are great services and with bitbucket, you can also create private repositories.

So that being said I thought it might be a good idea to keep a list of most common commands (obviously a subjective POV).

 

Git commands

Working tree status:

git status

 

Stage changes all for commit:

git add .

 

Commit changes to the local repository:

git commit -m "Commit message"

 

Push changes to the remote repository:

git push

 

Updates local repository with branches and/or tags along with the objects necessary to complete their histories:  

git fetch

 

Fetch and integrate with another repository or a local branch: 

git pull

 

List local branches:

git branch

 

List both remote-tracking branches and local branches:

git branch --all

 

Create a branch in local repository:

git branch name_of_your_new_branch

 

Create a branch in the local repository and switch to the new branch:

git checkout -b name_of_your_new_branch

 

Push branch from the local repository to the remote repository:

git push origin name_of_your_new_branch

 

Show remote branches

git branch -r

 

Delete local branch

git branch -d branch_name

 

Delete remote branch:

git push origin --delete branch_name

 

Rename local branch:

git branch -m old_branch_name new_branch_name

 

Rename remote branch:

1. Rename local branch: 

git branch -m old_branch_name new_branch_name

2. Delete remote branch: 

git push origin --delete old_branch_name

3. Push the new branch, set local branch to track the new remote:

git push --set-upstream origin new_branch_name

 

Switch branch:

git checkout branch_name

 

Fetch remote branch

git fetch origin branch_name

 

Pull remote branch

git pull origin branch_name

 

List tags:

git tag

 

Create annotated tag:

git tag -a tag_name -m "Some information"

 

Remove file from repository and file system:

git rm file_name

 

Remove file only from the repository, but not file system:

git rm --cached file_name

 

How to merge a branch into another branch:

1. Save your changes:

git commit

or

git stash

2. Check out the branch you want to be merged into your current branch

git checkout branch_name

3. Update branch, if needed:

git pull

4. Go back to your original branch:

git checkout original_branch_name

5. Merge:

git merge branch_name

 

Save your local modifications and reverts the working directory to match the HEAD commit:

git stash

 

Save your local modifications, including untracked files, and reverts the working directory to match the HEAD commit:

git stash --include-untracked

 

Remove a single stashed state from the stash list and apply it on top of the current working tree state:

git stash pop

 

View stashed files:

git stash list

 

Clear stashed files:

git stash clear

 

Rename a tag

git tag new old
git tag -d old
git push origin :refs/tags/old
git push --tags

 

Move date for the selected commit to the index (staging area) and the working area:

working area <– index <– repository

git reset --hard <SOME-COMMIT>

 

Moves date for the selected commit to the index (staging area) but not the working area (this is the default option of reset):

index <– repository

git reset --mixed <SOME-COMMIT>

 

Move the branch to the selected commit, and do not touch the index and working area:

git reset --soft <SOME-COMMIT>

 

Unstage a file (similar to git reset –mixed <SOME-COMMIT>), modifies only the index (staging) area:

git reset HEAD <SOME-FILE>

 

Unstage a file (similar to git reset –hard<SOME-COMMIT>), modifies the index and the working area (use with care, this is a destructive operation):

git reset --hard HEAD <SOME-FILE>

 

 

To be continued…

Spread the knowledge