Git is a powerful version control system that helps developers track changes in their code and collaborate with others. Whether you’re new to Git or a seasoned pro, this comprehensive guide will help you understand the key concepts, commands, and troubleshooting tips you need to be successful.
What is Git?
Git is a distributed version control system that allows multiple people to work on the same codebase simultaneously. Git tracks changes to your code over time, so you can easily see what changes were made and by whom. With Git, you can collaborate with others on the same codebase without fear of overwriting each other’s work.
Git Commands
Git has a wide range of commands that you can use to manage your codebase. Here are some of the most common Git commands and what they do:
Cloning a repository
The first step in working with Git is to clone a repository. Cloning a repository creates a copy of the repository on your local machine, so you can work on the code locally.
git clone
For example, to clone the Git repository, you would use:
git clone https://github.com/git/git.git
How to checkout Git, create branch, push
1. switch head branch
git checkout
2. create new branch
git branch
git checkout -b
Note: The easiest way to create a Git branch is to use the “git checkout” command with the “-b” option for a new branch. Next, you just have to specify the name for the branch you want to create.
Checking the status of your code
Once you have a repository on your local machine, you can check the status of your code using the status command. This will show you any changes that have been made to your code since the last commit.
git status
Adding changes to the staging area
To commit changes to Git, you must first add them to the staging area. This tells Git which changes you want to commit.
git add
For example, to add a file named index.html, you would use:
git add index.html
git add web.html
git add -A or git add –-all
# this will add all these changes to the staging area using a single command i.e.
It recursively added all the changes i.e. new/modified/deleted files in folders and in subfolders to the staging area of git. We can confirm this by checking the git status of the project i.e.
Committing changes
After you’ve added changes to the staging area, you can commit them to the repository.
git commit -m "commit message"
# For example, to commit changes with the message "Updated index.html", you would use:
git commit -m "Updated index.html"
git commit -m "changes"
git commit -a is a shortcut to add all the modified tracked files to the index first.
Pushing changes
Once you’ve committed your changes, you can push them to the remote repository.
git push
#This will push all committed changes to the default remote repository.
git push origin
“fatal: The current branch akim-gocd has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin akim-gocd
“
git pull git@github.com:yourrepo/chef.git
Troubleshooting Git Issues
While Git is a powerful tool, it can sometimes be difficult to troubleshoot issues that arise. Here are some common Git issues and how to fix them.
Git not recognizing changes.
If Git does not recognize changes you’ve made to a file, you may need to add the changes to the staging area.
git add
Merge conflicts
When two people make changes to the same file, a merge conflict can occur. This happens when Git is unable to automatically merge the changes.
To resolve a merge conflict, you need to manually edit the file to resolve the conflict. Once you’ve resolved the conflict, you can commit the changes.
Pushing to a protected branch
If you try to push changes to a protected branch, you may receive an error message. To push changes to a protected branch, you need to have the proper permissions.
Authentication issues
If you’re having authentication issues with Git, you may need to check your credentials. You may need to enter your username and password or set up an SSH key.
Conclusion
Git is a powerful tool that can help you manage your codebase and collaborate with others. By using the common Git commands and following these troubleshooting tips, you can ensure your Git workflow is efficient and error-free.
Other Git tips and commands
To sync from git repo to local
=====
Reset and sync local repository with remote branch
git fetch origin
git reset --hard origin/master (change master to your branch)
git clean -f -d
git fetch –prune
# sync all branch local and remote
# sync all together in one line command
git fetch origin && git reset --hard origin/master && git clean -f -d
-------------
Note: to add syntax code to the github
```javascript
```
1.changed files in your working directory
git status
2. changes to tracked files
git diff
3. add current changes to next commit
git add .
4. add some changes in file to next commit
git add -p
5. commit all local changes in tracked files
git commit -a
git commit -m “comment”
6. commit previously staged changes
git commit
7. change the last commit
git commit —amend
Commit History
=====
1. show all commits, starting with newest
git log
2. show changes over time for a specific file
git log -p
3. who changes what and when in
git blame
Branches & Tags
1. list all existing branches
git branch -av
2. switch head branch
git checkout
3. create new branch
git branch
4. create a new tracking branch based on a remote branch
git checkout —track
5. delete local branch
Git auto clone/pull via api with token
mkdir dir-name
cd dir-name
git init
## this only work for master branch - use pull or clone
git pull https://:x-oauth-basic@github.com//.git
## This only for specific branch - use clone
git clone https://:x-oauth-basic@github.com//.git --branch=
Switching user in github
Viewing Current Git User
In order to see the current git, enter this:
git config user.email
Changing To a Different Git User
If you want to switch to a new user, just call the same command and pass in a string of the email address. I recommend using the the --global flag, but it's not always necessary.
git config --global user.email "myemail@gmail.com"
Add your new user key to ssh-key agent with key location
ssh-add -K ~/.ssh/id_rsa
git clone git@github.com:repo/dummy-test.git
Discard local changes
git reset --hard
Remove your uncommitted changes
If you want to completely remove the file from the index, you will have to use the “git rm” command with the “–cached” option
$ git rm --cached
Also, run
git reset –hard
To stop tracking a file, we must remove it from the index:
git rm --cached
To remove a folder and all files in the folder recursively:
git rm -r --cached
Tracked files
git checkout -f
Untracked files
git clean -fd
Save your changes for later
Tracked files
git stash
Tracked files and untracked files
git stash -u
Reapply your latest stash after git pull:
git stash pop
Git
NOTE:
If you get error “fatal: unable to auto-detect email address (got xxx')”
Add the following to your ~/.gitconfig
[user]
name = “your name”
email = “your email as in github”