A Practical Guide to Git
Working with Git on the command line can be daunting. To help with that, I’ve put together a list of common Git commands. Here you will learn the basics of using git, as well as some more advanced commands that you should find useful!
1. Create a new repository
2. Adding a file to a repository
3. Cloning a repository and Track all remote branches
4. Create and delete branches
5. Delete all merged branches
6. Rename a local and remote branch
7. Stash and restore changes
8. Reset a branch to the remote
9. Changing a remote’s URL
10. Tag
11. Removing the last commit
12. Reworking the last commit
13. Updating the last commit message
14. Rebase
15. Fire Commit
1. Create a new repository
Initialize the local directory as a Git repository:
git init
Add the files in your new local repository:
git add .
Commit the files that you’ve staged in your local repository:
git commit -m "<commit message>"
Add the URL for the remote repository:
git remote add origin <remote repository URL>
git remote -v //Verifies the new remote URL
Push the changes in your local repository to GitHub:
git push -u origin master
2. Adding a file to a repository
Stage the file for commit to your local repository:
git add .
Commit the file that you’ve staged in your local repository:
git commit -m "<commit message>"
Push the changes in your local repository to GitHub:
git push origin <branch-name>
3. Cloning a repository
git clone <remote url>
Track all remote branches:
#!/bin/bash
for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
git branch --track "${branch##*/}" "$branch"
donegit fetch --all
git pull --all
4. Create and delete branches
To create your branch:
git pull --rebase
git checkout -b <branchName>
To push a local git branch to remote:
git push -u origin <branch-name>
To delete a local branch:
git branch -d <branch-name>
To delete a remote branch:
git push origin --delete <branch-name>
5. Delete all merged branches
To list locally-tracking branches that were merged in remote
git branch --merged
Add few arguments to skip important branches that don’t want to delete like master or a develop. The following command will not delete master, dev and skip_branch_name.
git branch --merged | egrep -v "(^\*|master|dev|skip_branch_name)"
6. Rename a local and remote branch
Rename your local branch:
git branch -m <new-name>
If you are on a different branch:
git branch -m <old-name> <new-name>
Delete the old-name remote branch and push the new-name local branch:
git push origin :<old-name> <new-name>
Reset the upstream branch for the new-name local branch. Switch to the branch and then:
git push origin -u <new-name>
7. Stash and restore changes
How to stash changes in git:
git stash //Save all local changes
git stash --patch //Save a specific file via interactive patch mode
How to restore stashed changes:
git apply //Preserve changes in the stack
git stash pop //Discard changes from the stack.
8. Reset a branch to the remote
Confirm local changes:
git status
Reset to the latest commit on remote / upstream:
git reset --hard HEAD
9. Changing a remote’s URL
The git remote set-url
command changes an existing remote repository URL.
Change the current working directory to your local project.
git remote -v // View existing remotes// Change the 'origin' remote's URL
git remote set-url origin <remote-url>git remote -v // Verify new remote URL
10. Tag
To create a lightweight tag:
A lightweight tag is very much like a branch that doesn’t change — it’s just a pointer to a specific commit.
git tag <tag-name>
To create an annotated tag:
Annotated tags, however, are stored as full objects in the Git database. They’re checksummed; contain the tagger name, email, and date; have a tagging message. You can see the tag data along with the commit that was tagged by using the git show
command.
git tag -a <tag-name> -m "tagging-message"
To push local tags to remote:
git push origin <tag-name>
If you have a lot of tags that you want to push up at once, you can also use:
git push --tags
Another options, links local and remote tags:
git push --follow-tags
Tagging later:
git tag -a <tag-name> <commit-id>
To delete a local tag:
git tag -d <tag-name>
To delete a remote tag:
git tag -d <tag-name>
git push -d origin <tag-name>
11. Removing the last commit
To remove the last commit:
git reset --hard HEAD^
Removing multiple commits from the top:
You can increase the number to remove even more commits.
git reset --hard HEAD~2 //remove the last two commits.
To update changes in remote:
git push origin -f <branch-name>
12. Reworking the last commit
To perform significant work on the last commit:
git reset HEAD^
13. Updating the last commit message
Commit has not been pushed:
git commit --amend
Commit has been pushed:
git commit --amend
git push origin -f <branch-name>
14. Rebase
Standard rebasing:
Automatically take the commits in your current working branch and apply them to the head of the passed branch.
git rebase
Interactive rebasing:
git rebase --i
This opens an editor where you can enter commands (described below) for each commit to be rebased. These commands determine how individual commits will be transferred to the new base. You can also reorder the commit listing to change the order of the commits themselves. Once you’ve specified commands for each commit in the rebase, Git will begin playing back commits applying the rebase commands. The rebasing edit commands are as follows:
15. Fire Commit
In case of 🔥:
git commit
git push
git leave building
Did you like this?
Recommend this post (by clicking 👏 the button) so other people can see it too…
I can also connect on Twitter @anasaman_p