General Category > General Discussion

Git Cheat Sheet

(1/1)

aravindm:
Git is a version-control system for tracking changes in computer files and coordinating work on those files among multiple people. It is primarily used for source-code management in software development, but it can be used to keep track of changes in any set of files.

Set Up and Initialization

Check your Git version with the following command, which will also confirm that Git is installed.

--- Code: ---git --version
--- End code ---

You can initialize your current working directory as a Git repository with init.

--- Code: ---git init
--- End code ---

To copy an existing Git repository hosted remotely, you’ll use git clone with the repo’s URL or server location (in the latter case you will use ssh).


--- Code: ---git clone https://www.github.com/username/repo-name
--- End code ---

Show your current Git directory’s remote repository.

--- Code: ---git remote
--- End code ---

For a more verbose output, use the -v flag.

--- Code: ---git remote -v
--- End code ---

Add the Git upstream, which can be a URL or can be hosted on a server (in the latter case, connect with ssh).

--- Code: ---git remote add upstream https://www.github.com/username/repo-name
--- End code ---


Staging

When you’ve modified a file and have marked it to go in your next commit, it is considered to be a staged file.

Check the status of your Git repository, including files added that are not staged, and files that are staged.

--- Code: ---git status
--- End code ---

To stage modified files, use the add command, which you can run multiple times before a commit. If you make subsequent changes that you want included in the next commit, you must run add again.

You can specify the specific file with add.

--- Code: ---git add my_script.py
--- End code ---

With . you can add all files in the current directory including files that begin with a ..

--- Code: ---git add .
--- End code ---

You can remove a file from staging while retaining changes within your working directory with reset.

--- Code: ---git reset my_script.py
--- End code ---


Committing

Once you have staged your updates, you are ready to commit them, which will record changes you have made to the repository.

To commit staged files, you’ll run the commit command with your meaningful commit message so that you can track commits.

--- Code: ---git commit -m "Commit message"
--- End code ---

You can condense staging all tracked files with committing them in one step.

--- Code: ---git commit -am "Commit message"
--- End code ---

If you need to modify your commit message, you can do so with the --amend flag.

--- Code: ---git commit --amend -m "New commit message"
--- End code ---


Branches

A branch in Git is a movable pointer to one of the commits in the repository, it allows you to isolate work and manage feature development and integrations.

List all current branches with the branch command. An asterisk (*) will appear next to your currently active branch.

--- Code: ---git branch
--- End code ---

Create a new branch. You will remain on your currently active branch until you switch to the new one.

--- Code: ---git branch new-branch
--- End code ---

Switch to any existing branch and check it out into your current working directory.

--- Code: ---git checkout another-branch
--- End code ---

You can consolidate the creation and checkout of a new branch by using the -b flag.

--- Code: ---git checkout -b new-branch
--- End code ---

Rename your branch name.

--- Code: ---git branch -m current-branch-name new-branch-name
--- End code ---

Merge the specified branch’s history into the one you’re currently working in.

--- Code: ---git merge branch-name
--- End code ---

Abort the merge, in case there are conflicts.

--- Code: ---git merge --abort
--- End code ---

You can also select a particular commit to merge with cherry-pick with the string that references the specific commit.

--- Code: ---git cherry-pick f7649d0
--- End code ---

When you have merged a branch and no longer need the branch, you can delete it.

--- Code: ---git branch -d branch-name
--- End code ---

If you have not merged a branch to master, but are sure you want to delete it, you can force delete a branch.

--- Code: ---git branch -D branch-name
--- End code ---


Collaborate and Update

To download changes from another repository, such as the remote upstream, you’ll use fetch.

--- Code: ---git fetch upstream
--- End code ---

Merge the fetched commits.

--- Code: ---git merge upstream/master
--- End code ---

Push or transmit your local branch commits to the remote repository branch.

--- Code: ---git push origin master
--- End code ---

Fetch and merge any commits from the tracking remote branch.

--- Code: ---git pull
--- End code ---


Inspecting

Display the commDisplay the commit history for the currently active branch.

--- Code: ---git log
--- End code ---

Show the commits that changed a particular file. This follows the file regardless of file renaming.

--- Code: ---git log --follow my_script.py
--- End code ---

Show the commits that are on one branch and not on the other. This will show commits on a-branch that are not on b-branch.

--- Code: ---git log a-branch..b-branch
--- End code ---

Look at reference logs (reflog) to see when the tips of branches and other references were last updated within the repository.

--- Code: ---git reflog
--- End code ---

Show any object in Git via its commit string or hash in a more human-readable format.

--- Code: ---git show de754f5
--- End code ---


Show Changes

The git diff command shows changes between commits, branches, and more.

Compare modified files that are on the staging area.

--- Code: ---git diff --staged
--- End code ---

Display the diff of what is in a-branch but is not in b-branch.

--- Code: ---git diff a-branch..b-branch
--- End code ---

Show the diff between two specific commits.

--- Code: ---git diff 61ce3e6..e221d9c
--- End code ---


Stashing

Sometimes you’ll find that you made changes to some code, but before you finish you have to begin working on something else. You’re not quite ready to commit the changes you have made so far, but you don’t want to lose your work. The git stash command will allow you to save your local modifications and revert back to the working directory that is in line with the most recent HEAD commit.

Stash your current work.

--- Code: ---git stash
--- End code ---

See what you currently have stashed.

--- Code: ---git stash list
--- End code ---

Your stashes will be named stash@{0}, stash@{1}, and so on.

Show information about a particular stash.

--- Code: ---git stash show stash@{0}
--- End code ---

To bring the files in a current stash out of the stash while still retaining the stash, use apply.

--- Code: ---git stash apply stash@{0}
--- End code ---

If you want to bring files out of a stash, and no longer need the stash, use pop.

--- Code: ---git stash pop stash@{0}
--- End code ---

If you no longer need the files saved in a particular stash, you can drop the stash.

--- Code: ---git stash drop stash@{0}
--- End code ---

If you have multiple stashes saved and no longer need to use any of them, you can use clear to remove them.

--- Code: ---git stash clear
--- End code ---


Ignoring Files

If you want to keep files in your local Git directory, but do not want to commit them to the project, you can add these files to your .gitignore file so that they do not cause conflicts.

Use a text editor such as nano to add files to the .gitignore file.

--- Code: ---nano .gitignore
--- End code ---


Rebasing

A rebase allows us to move branches around by changing the commit that they are based on. With rebasing, you can squash or reword commits.

You can start a rebase by either calling the number of commits you have made that you want to rebase (5 in the case below).

--- Code: ---git rebase -i HEAD~5
--- End code ---

Alternatively, you can rebase based on a particular commit string or hash.

--- Code: ---git rebase -i 074a4e5
--- End code ---

Once you have squashed or reworded commits, you can complete the rebase of your branch on top of the latest version of the project’s upstream code.

--- Code: ---git rebase upstream/master
--- End code ---

Resetting

Sometimes, including after a rebase, you need to reset your working tree. You can reset to a particular commit, and delete all changes, with the following command.


--- Code: ---git reset --hard 1fc6665
--- End code ---

To force push your last known non-conflicting commit to the origin repository, you’ll need to use --force.

--- Code: ---git push --force origin master
--- End code ---

To remove local untracked files and subdirectories from the Git directory for a clean working branch, you can use git clean.

--- Code: ---git clean -f -d
--- End code ---

If you need to modify your local repository so that it looks like the current upstream master (that is, there are too many conflicts), you can perform a hard reset.

--- Code: ---git reset --hard upstream/master
--- End code ---

Navigation

[0] Message Index

Go to full version